package cn.yhjz.admin.biz.controller;

import cn.yhjz.admin.biz.service.PersonService;
import cn.yhjz.admin.biz.entity.Person;
import cn.yhjz.admin.biz.vo.PersonVo;
import cn.yhjz.common.AjaxResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/biz/person")
public class PersonController {

    @Autowired
    private PersonService personService;

    @PreAuthorize("hasAuthority('biz:person:query')")
    @PostMapping("/list")
    public List<PersonVo> list(@RequestBody Person person) {
        return personService.select(person);
    }

    @PreAuthorize("hasAuthority('biz:person:query')")
    @PostMapping("/page")
    public AjaxResponse page(@RequestBody PersonVo vo) {
        Person entity = new Person();
        BeanUtils.copyProperties(vo, entity);
        return AjaxResponse.success(personService.getPage(vo.getCurrentPage(), vo.getPageSize(), entity));
    }

    @PreAuthorize("hasAuthority('biz:person:add')")
    @PostMapping("/add")
    public AjaxResponse add(@RequestBody Person entity) {
        entity.setId(null);
        return AjaxResponse.success(personService.insert(entity));
    }

    @PreAuthorize("hasAuthority('biz:person:edit')")
    @PostMapping("/edit")
    public AjaxResponse edit(@RequestBody Person entity) {
        if (null == entity.getId()) {
            return AjaxResponse.fail("缺少主键");
        }
        return AjaxResponse.success(personService.updateById(entity));
    }

    @PreAuthorize("hasAuthority('biz:person:query')")
    @GetMapping("/selectById")
    public AjaxResponse selectById(Long id) {
        return AjaxResponse.success(personService.selectById(id));
    }

    @PreAuthorize("hasAuthority('biz:person:delete')")
    @DeleteMapping("/deleteById")
    public AjaxResponse deleteById(Long id) {
        return AjaxResponse.success(personService.deleteById(id));
    }
}
